<?

/*	Text.news OBJECT
*	Methods:
*	output
*
*	Constructor:
*
*
*					*/


class TextNews extends modTEXT {

	function __construct( $vars ) {
		
		$this->_addvars( $vars );
		$this->_ref();
	}
	
	public function output() {
	
		if( $this->action )
			$nodes = $this->{ "filter_" . $this->action }();
		
		
		if( $nodes ) $script = $this->check_js( $nodes ); 
		
		$result = ( $nodes ) ?
			"<ul>" . implode("\n", $nodes ) . "</ul>\n" . $script : "empty.";
	
	
		return $this->parse( $result );
	}
	
	
	private function filter_recent() {
		global $DB;
		
		if( !$this->vars->limit ) $this->vars->limit = 24; 
		$long_key = ( $this->vars->maxchars )? "concat( SUBSTRING( <%lang:contid:long%>, 1, {$this->vars->maxchars}), ' ...' )" : "<%lang:contid:long%>";
		
		$nodes = $DB->filter_key(
			"news",
			"active && 	date < now() order by date desc limit " . $this->vars->limit,
			array( "ID", "creator", "date", "date_format( date, '{$this->vars->dformat}') as dformat", "<%lang:nameid%> as name", "$long_key as content", "(select user from user where ID=creator) as user" ) );
		
		if( $nodes ) 
			foreach($nodes as $nkey => $node ) $nodes[ $nkey ] = $this->render_node( $node, (( $href = $this->vars->href )? $href : false )); 

		return $nodes;
	}


	private function filter_newsletter() {
		global $DB;
		
		if( !$this->vars->limit ) $this->vars->limit = 24; 
		$long_key = ( $this->vars->maxchars )? "concat( SUBSTRING( <%lang:contid:long%>, 1, {$this->vars->maxchars}), ' ...' )" : "<%lang:contid:long%>";
		
		$nodes = $DB->filter_key(
			"news",
			"active && 	date < now() order by date desc limit " . $this->vars->limit,
			array( "ID", "creator", "date", "date_format( date, '{$this->vars->dformat}') as dformat", "<%lang:nameid%> as name", "$long_key as content", "(select user from user where ID=creator) as user" ) );
		
		if( $nodes ) 
			foreach($nodes as $nkey => $node ) $nodes[ $nkey ] = $this->render_node_news( $node, (( $href = $this->vars->href )? $href : false )); 

		return $nodes;
	}


	private function filter_popular() {
		global $DB;
		
		/*if( !$this->vars->limit ) $this->vars->limit = 24; 
		$long_key = ( $this->vars->maxchars )? "concat( SUBSTRING( <%lang:contid:long%>, 1, {$this->vars->maxchars}), ' ...' )" : "<%lang:contid:long%>";
		
		$nodes = $DB->filter_key(
			"news",
			"active && 	date < now() order by date desc limit " . $this->vars->limit,
			array( "ID", "creator", "date", "date_format( date, '{$this->vars->dformat}') as dformat", "<%lang:nameid%> as name", "$long_key as content", "(select user from user where ID=creator) as user" ) );
		
		if( $nodes ) 
			foreach($nodes as $nkey => $node ) $nodes[ $nkey ] = $this->render_node( $node, (( $href = $this->vars->href )? $href : false )); 

		return $nodes;*/
	}


	private function render_node( $node, $href = false ) {

		return "<li><a name='{$node['ID']}'></a><span class='date'>{$node['dformat']}</span><" . (( $href )? "a href='" . $href . "'" : "span" ) . " class='name'>" . utf8_encode( $node['name'] ) . "</" . (( $href )? "a" : "span" ) . "><span class='content'>{$node['content']}</span><span class='nfo'>posted on {$node['date']}, by {$node['user']}</span></li>";
	}
	
	private function render_node_news( $node, $href = false ) {

		return "<li><b class='name'>{$node['name']}</b><br /><span class='content'>{$node['content']}</span></li>";
	}
	
	private function check_js( $nodes ) {
		
		$script = "mod = new TextNews( 'last' );";
		if		( $this->vars->expand == "first" )	$script .= "mod.expand('first');";
		else if	( $this->vars->expand == "all" )	$script .= "mod.expand();";
		else if	( is_int( $this->vars->expand ) )	$script .= "mod.expand({$this->vars->expand});";
		
		return  "<script type='text/javascript'>". $script ."</script>";
	}
	

} 

?>